<template>
    <div class="driver-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <company-select label="公司" custom-class="w-[320px]" @select-company="handleSelectCompany" />
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="queryParams.name" class="w-[280px]" clearable />
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="queryParams.mobile" class="w-[280px]" clearable />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getLists">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增司机
                </el-button>
                <el-button type="primary" @click="handleImport()">
                    <template #icon>
                        <icon name="el-icon-Upload" />
                    </template>
                    导入司机
                </el-button>
            </div>
            <el-table
                ref="tableRef"
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column label="公司" prop="company.company_name" min-width="180" />
                <el-table-column label="姓名" prop="name" min-width="100" />
                <el-table-column label="手机号" prop="mobile" min-width="120" />
                <el-table-column label="身份证" prop="license" min-width="180" />
                <el-table-column label="注册地编码" prop="region_code" min-width="120" />
                <el-table-column label="车牌号" prop="car.vehicle_no" min-width="120" />
                <!-- <el-table-column label="创建时间" prop="createTime" min-width="180" /> -->
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button type="primary" link @click="handleEdit(row)">
                            编辑
                        </el-button>
                        <el-button type="danger" link @click="handleDelete(row)">
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="driver">
import type { ElTable, FormInstance } from 'element-plus'
import EditPopup from './edit.vue'
import feedback from '@/utils/feedback'
import { driverLists, driverDelete } from '@/api/supervise/driver'
import CompanySelect from '@/components/company-select/index.vue'
import { usePaging } from '@/hooks/usePaging'

const tableRef = shallowRef<InstanceType<typeof ElTable>>()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const formRef = shallowRef<FormInstance>()
const showEdit = ref(false)

const queryParams = reactive({
    company_id: '',
    name: '',
    mobile: ''
})

// 使用 usePaging hook
const { pager, getLists, resetParams } = usePaging({
    fetchFun: driverLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (row: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(row)
}

const handleDelete = async (row: any) => {
    try {
        await feedback.confirm('确定要删除？')
        await driverDelete({ list: [row.id] })
        feedback.msgSuccess('删除成功')
        getLists()
    } catch (error) {
        console.error(error)
    }
}

const handleImport = () => {
    // TODO: 实现导入功能
}

const handleSelectCompany = (value: any) => {
    queryParams.company_id = value
}

onMounted(() => {
    getLists()
})
</script>